<template>
  <el-dialog
    :title="btnType==0?'新增':'修改'"
    :visible.sync="dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="所属角色" prop="role">
        <el-select v-model.number="ruleForm.role" placeholder="请选择所属角色">
          <el-option label="请选择" value></el-option>
          <el-option label="管理者" :value="0"></el-option>
          <el-option label="街道负责人" :value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="是否有效" prop="type">
        <el-radio-group v-model="ruleForm.type">
          <el-radio :label="0">是</el-radio>
          <el-radio :label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提 交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addTrafficApi, editTrafficApi } from "../../../api/trafficApi";
export default {
  props: ["row", "btnType"],
  data() {
    return {
      dialogVisible: true,
      ruleForm: {
        id: "",
        name: "",
        // personnelnum: 20050011,
        role: "", //0是管理者 1是街道负责人
        // explain: 0, //0是该账号拥有权限 1是上传车位信息
        // newdate: "2016-05-02 10:10:10",
        type: "" //0 是 1 否
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        role: [
          { required: true, message: "请选择所属角色", trigger: "change" }
        ],
        type: [{ required: true, message: "请选择是否生效", trigger: "change" }]
      }
    };
  },
  mounted() {
    //把点击的当前行数据赋值给弹框 要深拷贝
    // this.ruleForm={...this.row}
    this.ruleForm = JSON.parse(JSON.stringify(this.row));
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //true 验证通过

          alert("submit!");

          if (this.btnType == 0) {
            //新增逻辑
            // 获取表单的值 发送请求 后端添加逻辑 通知前端添加成功 刷新表格关闭弹框
            addTrafficApi(this.ruleForm).then(res => {
              console.log("添加api", res);
              if (res.code == 200) {
                this.$emit("resettable");
                this.$emit("parentclosedialog");
                this.$message({
                  message: res.msg,
                  type: "success"
                });
              }
            });
          } else {
            // 修改逻辑
            // 获取表单的值 发送请求 后端修改逻辑 通知前端修改成功 刷新表格关闭弹框
            editTrafficApi(this.ruleForm).then(res => {
              console.log("修改api", res);
              if (res.code == 200) {
                this.$emit("resettable");
                this.$emit("parentclosedialog");
                this.$message({
                  message: res.msg,
                  type: "success"
                });
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    closeDialog() {
      this.$emit("parentclosedialog");
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
          this.closeDialog();
        })
        .catch(_ => {});
    }
  }
};
</script>

<style>
</style>